package com.example.administrator.kotlin.activity

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentStatePagerAdapter
import com.example.administrator.kotlin.R
import com.example.administrator.kotlin.fragment.HomeFragment
import com.example.administrator.kotlin.fragment.NewsFragment
import kotlinx.android.synthetic.main.activity_bili.*

class BiliBiliActivity : AppCompatActivity() {
    var homeFragment: HomeFragment = HomeFragment()
    var newsFragment: NewsFragment = NewsFragment()
    var pageradapter: ViewPagerAdapter? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_bili_bili)
        var fragment = arrayOf<Fragment>(homeFragment, newsFragment)
        var titles = arrayOf("home", "news")
        pageradapter = ViewPagerAdapter(supportFragmentManager, fragment, titles)
        viewpager.adapter = pageradapter
        tablayout.setupWithViewPager(viewpager)
    }

    class ViewPagerAdapter(var supportFragmentManager: FragmentManager, var fragment: Array<Fragment>, var titles: Array<String>) :
            FragmentStatePagerAdapter(supportFragmentManager) {
        override fun getItem(position: Int): Fragment {
            return fragment.get(position)
        }

        override fun getCount(): Int {
            return fragment.size
        }

        override fun getPageTitle(position: Int): CharSequence {
            return titles[position]
        }


    }
}
